package com.gkzf.ai.module.crm.service.product.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkzf.ai.framework.common.exception.ServiceException;
import com.gkzf.ai.framework.common.util.object.BeanUtils;
import com.gkzf.ai.module.crm.config.BaseService;
import com.gkzf.ai.module.crm.controller.admin.product.vo.productadmittancecondition.ProductAdmittanceConditionRespVO;
import com.gkzf.ai.module.crm.controller.admin.product.vo.productadmittancecondition.ProductAdmittanceConditionSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.product.ProductAdmittanceConditionDO;
import com.gkzf.ai.module.crm.dal.mysql.product.CrmProductMapper;
import com.gkzf.ai.module.crm.dal.mysql.product.ProductAdmittanceConditionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.gkzf.ai.module.crm.enums.ErrorCodeConstants.PRODUCT_ADMITTANCE_CONDITION_ID_NOT_EXISTS;
import static com.gkzf.ai.module.crm.enums.ErrorCodeConstants.PRODUCT_ADMITTANCE_CONDITION_NOT_EXISTS;

/**
 * CRM 产品准入条件 Service 实现类
 *
 * @author 国科智飞
 */
@Service
@Validated
public class ProductAdmittanceConditionServiceImpl extends BaseService<ProductAdmittanceConditionDO> implements ProductAdmittanceConditionService {

    private static final String COMMA = ",";

    @Resource
    private ProductAdmittanceConditionMapper productAdmittanceConditionMapper;

    @Resource
    private CrmProductMapper productMapper;

    @Override
    public Long createProductAdmittanceCondition(ProductAdmittanceConditionSaveReqVO createReqVO) {
        ProductAdmittanceConditionDO productAdmittanceCondition = BeanUtils.toBean(createReqVO, ProductAdmittanceConditionDO.class);
        isFieldRepetition(ProductAdmittanceConditionDO::getNo,productAdmittanceCondition.getNo(),PRODUCT_ADMITTANCE_CONDITION_NOT_EXISTS);
        // 插入产品准入条件表
        productAdmittanceConditionMapper.insert(productAdmittanceCondition);
        // 返回
        return productAdmittanceCondition.getId();
    }


    @Override
    public void updateProductAdmittanceCondition(ProductAdmittanceConditionSaveReqVO updateReqVO) {
        isIdExists(updateReqVO.getId(),PRODUCT_ADMITTANCE_CONDITION_ID_NOT_EXISTS);
        ProductAdmittanceConditionDO productAdmittanceCondition = BeanUtils.toBean(updateReqVO, ProductAdmittanceConditionDO.class);
        // 更新
        productAdmittanceConditionMapper.updateInfoById(productAdmittanceCondition);
    }

    /**
     * 修改产品合作机构 同步准入条件合作机构
     * @param productNo 产品编号
     * @param cooperationInstitution 合作机构
     */
    public void updateCooperationInstitution(String productNo,Integer cooperationInstitution){
        ProductAdmittanceConditionDO productAdmittanceConditionDO = new ProductAdmittanceConditionDO();
        productAdmittanceConditionDO.setCooperationInstitution(cooperationInstitution);
        LambdaQueryWrapper<ProductAdmittanceConditionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAdmittanceConditionDO::getNo, productNo);
        productAdmittanceConditionMapper.update(productAdmittanceConditionDO,queryWrapper);
    }

    /**
     * 删除准入条件
     * @param projectNo 编号
     */
    @Override
    public void deleteProductAdmittanceCondition(String projectNo) {
        // 删除
        LambdaQueryWrapper<ProductAdmittanceConditionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAdmittanceConditionDO::getNo, projectNo);
        productAdmittanceConditionMapper.delete(queryWrapper);
    }



    @Override
    public ProductAdmittanceConditionRespVO getProductAdmittanceCondition(String no) {
        // 校验存在
        LambdaQueryWrapper<ProductAdmittanceConditionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAdmittanceConditionDO::getNo, no);
        List<ProductAdmittanceConditionDO> productAdmittanceConditionDOList = productAdmittanceConditionMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(productAdmittanceConditionDOList)){
            return new ProductAdmittanceConditionRespVO();
        }
        return BeanUtils.toBean(productAdmittanceConditionDOList.get(0), ProductAdmittanceConditionRespVO.class);
    }


    @Override
    public Integer getProductAdmittanceConditionCount(String productNo) {
      try{
          isFieldRepetition(ProductAdmittanceConditionDO::getNo,productNo,PRODUCT_ADMITTANCE_CONDITION_NOT_EXISTS);
          return 0;
      }catch (ServiceException serviceException){
          return 1;
      }
    }


}